package com.dlwx.parttimetalent.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.ui.ChildClassifyActivity;
import com.dlwx.parttimetalent.ui.EditLabelDetailActivity;
import com.dlwx.parttimetalent.ui.HyyrTwoActivity;
import com.dlwx.parttimetalent.utils.LogUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/11/24.
 */

public class ExpandEditAdapter extends BaseExpandableListAdapter {
    private final LayoutInflater mInflater;
    public List<OrderRecBean.BodyBean> group ;
    private Context context;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            notifyDataSetChanged();//更新数据
            super.handleMessage(msg);
        }
    };


    public ExpandEditAdapter(List<OrderRecBean.BodyBean> group, Context context) {
        this.group = group;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }
    public List<OrderRecBean.BodyBean> getDataList(){
        return group;
    }

    public void setDataList(List<OrderRecBean.BodyBean> datas){

        if(group!=null){
            group.clear();
            group.addAll(datas);
            notifyDataSetChanged();
        }
    }
    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX> childList=null;
        if(group.size()>groupPosition){
            childList = group.get(groupPosition).getChild();
        }
        if(childList==null){
            return 0;
        }
        return childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX> childList = group.get(groupPosition).getChild();
        LogUtils.d("childList:"+childList.size());
        if(childList==null){
            return null;
        }
        return childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandEditAdapter.ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder=new ExpandEditAdapter.ViewHolder();
            convertView = mInflater.inflate(R.layout.expand_group, null);
            viewHolder.tvTitle= convertView.findViewById(R.id.tv_title);
            viewHolder.swStatus1=convertView.findViewById(R.id.sw_group);
            viewHolder.imageArrow=convertView.findViewById(R.id.image_arrow);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ExpandEditAdapter.ViewHolder) convertView.getTag();
        }
        if(isExpanded){
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_down);
        }else{
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_top);
        }
        viewHolder.swStatus1.setVisibility(View.GONE);
        String name=group.get(groupPosition).getClassname();
        if(TextUtils.equals(name,"自定义工作类别")){
            viewHolder.tvTitle.setText("自定义工作类别+");
            viewHolder.tvTitle.setTextColor(ContextCompat.getColor(context,R.color.colorAccent));
        }else {
            viewHolder.tvTitle.setText(name);
            viewHolder.tvTitle.setTextColor(ContextCompat.getColor(context,R.color.second_textcolor));
        }
//        if(groupPosition==(group.size()-1)){
//            convertView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            });
//        }else{
////            convertView.setOnClickListener(this);
//        }
        return convertView;
    }
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final OrderRecBean.BodyBean parentBean = group.get(groupPosition);
        List<OrderRecBean.BodyBean.ChildBeanX> childList = parentBean.getChild();
        ChildViewHolder childViewHolder=null;
                if(convertView==null){
                         childViewHolder=new ChildViewHolder();
                        convertView = mInflater.inflate(R.layout.expand_child_edit, null);
                        childViewHolder.tvEdit= convertView.findViewById(R.id.tv_edit);
                        childViewHolder.tvName=convertView.findViewById(R.id.tv_name);
                        childViewHolder.tvAdd=convertView.findViewById(R.id.tv_add);
                         childViewHolder.rlClass=convertView.findViewById(R.id.rl_class);
                    childViewHolder.tvDelete=convertView.findViewById(R.id.tv_delete);
                    childViewHolder.expandListview=convertView.findViewById(R.id.expand_listview);
                      convertView.setTag(childViewHolder);
                }else{
                    childViewHolder= (ChildViewHolder) convertView.getTag();
                }

                if(isLastChild){
                    childViewHolder.rlClass.setVisibility(View.GONE);
                    childViewHolder.tvAdd.setVisibility(View.VISIBLE);
                    childViewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            OrderRecBean.BodyBean bean= group.get(groupPosition);
                            if(bean.getLabelclassid()==15){
                                Intent i=new Intent(context,EditLabelDetailActivity.class);
                                i.setClass(context,HyyrTwoActivity.class);
                                context.startActivity(i);
                            }else{

                                Intent i=new Intent(context, ChildClassifyActivity.class);
                                i.putExtra("id",parentBean.getLabelclassid()+"");
                                i.putExtra("name",parentBean.getClassname());
                                context.startActivity(i);
                            }
                        }
                    });
                }else{
                    childViewHolder.rlClass.setVisibility(View.VISIBLE);
                    childViewHolder.tvAdd.setVisibility(View.GONE);
                }

                if(childList!=null&&!childList.isEmpty()){
                    final OrderRecBean.BodyBean.ChildBeanX childBean = childList.get(childPosition);
                    final int labelId=childBean.getLabelid();
                    final int labelType = childBean.getLabeltype();
                    final String name=childBean.getLabelname();
                    childViewHolder.tvName.setText(childBean.getLabelname());
                    childViewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            callBack.delete(groupPosition,childPosition,childBean.getLabelid());
                        }
                    });

                    childViewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent i=new Intent(context,EditLabelDetailActivity.class);
                            i.putExtra("flag", Constant.ZERO);
                            i.putExtra("id",labelId+"");
                            i.putExtra("type",labelType+"");
                            i.putExtra("name",name);
                            context.startActivity(i);
                        }
                    });
                }



        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private static class ViewHolder
    {
        TextView tvTitle;
        Switch swStatus1,swStatus2;
        ImageView imageArrow;
        boolean isOpen;

    }
    private static class ChildViewHolder{
        TextView tvEdit;
        TextView tvDelete;
        TextView tvAdd;
        TextView tvName;
        RelativeLayout rlClass;
        ExpandableListView expandListview;

    }
    private CallBack callBack;
    public void setCallBack(CallBack callBack){
        this.callBack=callBack;
    }
    public interface CallBack{
        void delete(int groupPosition,int childPosition,int labelId);
        void edit(int position);
    }

    /*供外界更新数据的方法*/
    public void refresh(ExpandableListView expandableListView, int groupPosition){
        handler.sendMessage(new Message());
        //必须重新伸缩之后才能更新数据
        expandableListView.collapseGroup(groupPosition);
        expandableListView.expandGroup(groupPosition);
    }

}
